package tzhomestudio.framework.shared.utils;

import java.lang.reflect.Field;

/**
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class ClassLoaderUtils {
    // Constants
    private static final String JAVA_LIBRARY_PATH_KEY = "java.library.path";

    public static void addPathToClassPath(String fullPath) {
        // it's dirty hack, but it's work ;)
        try {
            Field f = java.lang.ClassLoader.class.getDeclaredField("sys_paths");
            f.setAccessible(true);
            f.set(java.lang.ClassLoader.class, null);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

        if (OSValidator.isWindows()) {
            System.setProperty(JAVA_LIBRARY_PATH_KEY, System.getProperty(JAVA_LIBRARY_PATH_KEY) + ";" + fullPath);
        } else {
            System.setProperty(JAVA_LIBRARY_PATH_KEY, System.getProperty(JAVA_LIBRARY_PATH_KEY) + ":" + fullPath);
        }

        try {
            System.load(FileUtils.getWorkingDir());
        } catch (Throwable ex) {
            //ex.printStackTrace();
        }
    }
}
